<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller
{
    function __construct()
    {
	parent::__construct();
    }
    
    function index()
    {
	if($this->_checkAuth()){
	    redirect('admin/question');
	}else{
	    redirect('admin/login');
	}
    }
    
    function login()
    {
	if($this->_checkAuth()){
	    redirect('admin/question');
	    exit;
	}
	
	if ($this->input->is_ajax_request()) {
	    $user = $this->input->post('username', TRUE);
	    $pass = $this->input->post('password', TRUE);
	    $return = array();
	    if ($user == "bmteam" && $pass == "bmpass") {
		$user = array(
		    'user' => 'admin',
		    'access' => date('Y-m-d H:i:s')
		);
		$this->session->set_userdata(array('admin' => $user));
		$return['status'] = true;
		$return['session'] = $this->session->userdata('admin');
	    } else {
		$return['status'] = false;
		$return['message'] = 'invalid username or password please try again';
	    }
	    echo json_encode($return);
	} else {
	    $this->mytpl->set_template('admin-bootstrap/login.html');
	    $this->mytpl->clear_cache();
	    $this->mytpl->display();
	}
    }
    
    /**
   * logout
   */
    function logout() {
	$this->session->unset_userdata('admin');
	redirect('admin');
    }
    
    function dashboard()
    {
	if( ! $this->_checkAuth()){
	    redirect('admin/login');
	    exit;
	}
	
	$this->mytpl->set_template('admin-bootstrap/question.html');
	$this->mytpl->clear_cache();
	$this->mytpl->display();
    }
    /**
    * private function to use in this controller
    */
    function _checkAuth() {
	$admin = $this->session->userdata('admin');
	if (isset($admin['user']) && $admin['user'] == 'admin') {
	    return true;
	} else {
	    return false;
	}
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */